package com.diqizu.servlet.admin;



import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;
import com.diqizu.dao.NewsTypeDaoImpl;
import com.diqizu.entity.NewsType;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;



/**
 * Servlet implementation class NewsServletAdmin
 */
@WebServlet("/admin/NewsTypeServletAdmin")
public class NewsTypeServletAdmin extends BaseServlet {

	NewsTypeDaoImpl newsTypeDao = new NewsTypeDaoImpl();
	/**
	 * 获取News类型
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNewsType(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 调用dao层的方法，获取所有员工
		ArrayList<NewsType> news = newsTypeDao.getAllList();
		
		//调用结果工具类，将数据返回
		String res = Result.toClient(0, "新闻列表", news,news.size());
		PrintWriter out = resp.getWriter(); //获取输出对象
		out.print(res); //输出数据
		out.flush();  //刷新
		out.close();  //关闭
	}
	
	/**
	 * 添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void insertNewsType(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//从页面上获取数据
		String name = req.getParameter("name");//表单中name的值
		
		// 调用dao层的方法，获取所有员工
		NewsType typeDao = new NewsType(0,name);
		boolean res = newsTypeDao.insertnewstype(typeDao);
		
		//调用结果工具类，将数据返回页面
		PrintWriter out = resp.getWriter(); //获取输出对象
		if(res) {
			out.write(Result.toClient(0, "添加成功"));
		}else {
			out.write(Result.toClient(1, "添加失败"));
		}
		out.flush();  //刷新
		out.close();  //关闭
	}
	
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateNewsType(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//从页面上获取数据
		String name = req.getParameter("name");//表单中name的值
		String id = req.getParameter("id");
		
		int ids = 0;
		if(id!=null && !id.equals("")) {
			ids = Integer.valueOf(id);
		}
		
		// 调用dao层的方法，获取所有员工
		NewsType typeDao = new NewsType(ids,name);

		boolean res = newsTypeDao.updatenewstype(typeDao);
		
		//调用结果工具类，将数据返回页面
		PrintWriter out = resp.getWriter(); //获取输出对象
		if(res) {
			out.write(Result.toClient(0, "修改成功"));
		}else {
			out.write(Result.toClient(1, "修改失败"));
		}
		out.flush();  //刷新
		out.close();  //关闭
	}
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delNewsType(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//从页面上获取数据
		String id = req.getParameter("id");
		
		int ids = 0;
		if(id!=null && !id.equals("")) {
			ids = Integer.valueOf(id);
		}
		
		// 调用dao层的方法，获取所有员工
		boolean res = newsTypeDao.delnewstype(ids);
		
		//调用结果工具类，将数据返回页面
		PrintWriter out = resp.getWriter(); //获取输出对象
		if(res) {
			out.write(Result.toClient(0, "删除成功"));
		}else {
			out.write(Result.toClient(1, "删除失败"));
		}
		out.flush();  //刷新
		out.close();  //关闭
	}
	
}
